package com.itmk.web.meteorological_station.controller;

import com.itmk.web.meteorological_station.entity.EnvironmentalStation;
import com.itmk.web.meteorological_station.entity.MeteorologicalStation;
import com.itmk.web.meteorological_station.service.DataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

@RestController
public class DataController {
    private static final Logger logger = LoggerFactory.getLogger(DataController.class);
    @Autowired
    private DataService dataService;

    // 根据变量名称和日期范围获取环境数据
    @GetMapping("/api/environmental_data")
    public List<EnvironmentalStation> getEnvironmentalData(

            @RequestParam String variableName,
            @RequestParam String startDate,
            @RequestParam String endDate) {
        // 打印接收到的变量名，确保它被正确传递
        logger.info("接收到的变量名: {}", variableName);
        // 根据变量名称来判断是否需要查询相应的环境数据
        Timestamp startTime = Timestamp.valueOf(startDate + " 00:00:00");
        Timestamp endTime = Timestamp.valueOf(endDate + " 23:59:59");

        List<EnvironmentalStation> result = new ArrayList<>();
        try {
            // 根据 variableName 判断并返回不同的数据
            switch (variableName) {
                case "WindDirection":
                case "LightIntensity":
                case "NegativeOxygen":
                    // 查询环境数据
                    result = dataService.getEnvironmentalDataByVariable(variableName, startTime, endTime);
                    break;
                default:
                    logger.warn("未知变量: {}", variableName);  // 打印未知变量的日志
                    break;
            }
        } catch (Exception e) {
            logger.error("Error while fetching environmental data", e);
        }

        if (result.isEmpty()) {
            logger.warn("No data found for the given variable: {} and date range: {} to {}", variableName, startDate, endDate);
        }

        return result;  // 返回查询结果
    }

    // 根据变量名称和日期范围获取气象数据
    @GetMapping("/api/meteorological_data")
    public List<MeteorologicalStation> getMeteorologicalData(
            @RequestParam(defaultValue = "Temperature") String variableName,
            @RequestParam String startDate,
            @RequestParam String endDate) {
        // 打印接收到的请求参数
        logger.info("Received request for variable: {}, startDate: {}, endDate: {}", variableName, startDate, endDate);
        Timestamp startTime = Timestamp.valueOf(startDate + " 00:00:00");
        Timestamp endTime = Timestamp.valueOf(endDate + " 23:59:59");
        List<MeteorologicalStation> result = new ArrayList<>();
        try {
            // 查询数据
            result = dataService.getMeteorologicalDataByVariable(variableName, startTime, endTime);
        } catch (Exception e) {
            logger.error("Error while fetching meteorological data", e);
        }

        // 打印返回的数据
        logger.info("Returning meteorological data: {}", result);

        if (result == null || result.isEmpty()) {
            logger.warn("No data found for the given range and variable.");
        } else {
            logger.info("Data found: {}", result.size());
        }

        return result;  // 返回查询结果
    }
}